home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************************/
- /* */
- /* Module: cspiff.c - neaten the appearance of C source files. */
- /* */
- /* Programmer: George R. Woodside */
- /* */
- /* Date: October 25, 1986 */
- /* */
- /* Flags: -i file Specify name of input file. */
- /* Default is stdin. */
- /* (If a parameter appears with no flag, it */
- /* is accepted as the name of the input file.) */
- /* -o file Specify name of output file. */
- /* Default is stdout. */
- /* -l int Set left margin for aligning comments. */
- /* Default is column 40. */
- /* -r int Set right margin for aligning comments. */
- /* Default is column 78. */
- /* -t int Set tab (indentation) value. */
- /* Default is 2 columns. */
- /* */
- /****************************************************************************/
-
- /****************************************************************************/
- /* */
- /* Line types: */
- /* (In line types, the symbol 'b' represents a blank space.) */
- /* */
- /* Full width comment line: */
- /* Starts in first column with /** */
- /* */
- /* Left aligned comment line: */
- /* Starts in first column with /*b */
- /* */
- /* Indented full width comment line: */
- /* Starts with /** in any column other than the first. */
- /* */
- /* Indented comment line: */
- /* Starts with /*b in any column other than the first. */
- /* Or follows a non-terminated comment line. */
- /* */
- /* Normal code line: */
- /* starts in the same column as the previous code line. */
- /* */
- /* Indentation Increase code line: */
- /* starts in any column further in than the previous code line. */
- /* */
- /* Indentation Decrease code line: */
- /* starts in any column further out than the previous code line. */
- /* */
- /****************************************************************************/
-
- #include <stdio.h>
-
- #define MAXLINE 256 /* width of a program line */
- #define BLANK 0 /* blank line */
- #define FCOMM 1 /* comment: full width */
- #define LCOMM 2 /* comment: left aligned */
- #define IFCOMM 3 /* comment: indented full width */
- #define ICOMM 4 /* comment: indented */
- #define CCOMM 5 /* comment: continued across lines */
- #define CODE 6 /* code: aligned */
- #define ICODE 7 /* code: indentation increase */
- #define OCODE 8 /* code: indentation decrease */
- #define O2CODE 9 /* code: decrease indentation twice */
- #define ZCODE 10 /* code: left aligned */
-
- extern char *optarg; /* argument pointer */
- extern int optind; /* index of unprocessed argument */
-
- char iline[MAXLINE+1]; /* input line */
- char oline[MAXLINE+1]; /* output line */
- char *pname = "cspiff"; /* program name */
-
- char *infile; /* input file name */
- char *outfile; /* output file name */
-
- int left = 40; /* left alignment of comments */
- int right = 78; /* right alignment of comments */
- int tab = 2; /* indentation value */
- int prior[32]; /* start of prior line addresses */
- int pindex = 0; /* start of prior line array index */
- int lowest; /* lowest position with data */
- int highest; /* highest position with data */
- int align; /* code alignment position */
- int i_flag = 0; /* input file present flag */
- int o_flag = 0; /* output file present flag */
- int c_flag = 0; /* processing continued comment */
-
- main(argc,argv)
- int argc;
- char *argv[];
- {
- FILE *fp_in;
-
- register int c; /* input byte */
-
- /***************************************************************************/
- /* */
- /* If unix system, save program name. */
- /* */
- /***************************************************************************/
-
- #ifdef UNIX
- pname = argv[0]; /* name is always first parameter. */
- #endif
-
- if(argc < 2)
- {
- printf("Usage: %s -i file -l int -r int -o file -t int\n",pname);
- printf("\t-i file = input file name\n");
- printf("\t-l int = left alignment of comments\n");
- printf("\t-r int = right alignment of comments\n");
- printf("\t-o file = output file name\n");
- printf("\t-t file = indentation tab value\n");
- exit(1);
- }
-
- while((c=getopt(argc,argv,"I:L:R:O:T:i:l:r:o:t:")) != EOF)
- {
- switch (c)
- {
- case('I'):
- case('i'):
- i_flag = 1; /* show file named */
- infile = optarg; /* input file name */
- break;
-
- case('L'):
- case('l'):
- left = atoi(optarg); /* left alignment */
- break;
-
- case('R'):
- case('r'):
- right = atoi(optarg); /* right alignment */
- break;
-
- case('O'):
- case('o'):
- o_flag = 1; /* show name is set */
- outfile = optarg; /* input file name */
- break;
-
- case('T'):
- case('t'):
- tab = atoi(optarg); /* indentation value */
- break;
-
-
- } /* end switch */
- } /* end while */
-
-
- while(optind != argc) /* if leftover arguments, */
- {
- if(i_flag == 0)
- {
- infile = argv[optind++];
- i_flag = 1;
- }
-
- else
- printf("%s: Unknown argument: %s\n",pname,argv[optind++]);
-
- } /* end while */
-
- if(i_flag)
- {
- if( (fp_in = fopen(infile,"r") ) == NULL)
- {
- printf("%s: Unable to open input file %s\n",pname,infile);
- exit(1);
- } /* end open error */
- else
- spiffo(fp_in); /* do the file */
-
- } /* end if i_flag */
- else
- spiffo(stdin); /* do standard in */
-
- } /* end main */
-
- /****************************************************************************/
- /* */
- /* Now we try to open the output file if needed */
- /* */
- /****************************************************************************/
-
- spiffo(fin)
- FILE *fin;
- {
- FILE *fout;
-
- if(o_flag)
- {
- if( (fout = fopen(outfile,"w") ) == NULL)
- {
- printf("%s: Unable to open output file %s\n",pname,outfile);
- exit(1);
- } /* end open error */
- else
- spiff(fin,fout); /* do the file */
-
- } /* end if o_flag */
- else
- spiff(fin,stdout); /* do standard out */
-
- }
-
- /****************************************************************************/
- /* */
- /* Now we process the file */
- /* */
- /****************************************************************************/
-
- spiff(ifp,ofp)
- FILE *ifp;
- FILE *ofp;
- {
-
- int type; /* type of line it is */
-
- prior[0] = 0; /* insure initial alignment */
-
- while( fgets(iline,MAXLINE -1,ifp) != NULL)
- {
- type = test(oline,iline); /* get the line type */
- process(type,iline,oline,ofp); /* process it */
- } /* end while we read a line */
-
- fclose(ifp); /* close input file */
- fclose(ofp); /* and the output */
- } /* end spiff */
-
- /****************************************************************************/
- /* */
- /* determine the type of a program line */
- /* */
- /****************************************************************************/
-
- test(linetmp,line)
- char line[];
- char linetmp[];
- {
-
- int c;
- int length = 0;
-
- if( detab(linetmp,line) == 0 ) /* if no data in line, */
- return(BLANK); /* call it blank */
-
- if(c_flag) /* if old comment did not end, */
- return(CCOMM); /* then continued comment */
-
- lowest = -1; /* no data found yet */
-
- while( (c = linetmp[length]) != NULL)
- {
-
- if( c != ' ')
- {
- if(lowest == -1)
- lowest = length; /* save left position */
- highest = length; /* save the address */
- } /* end not space */
-
- length++; /* and move up */
-
- } /* end while not end of line */
-
-
- if(linetmp[lowest] == '/')
- {
- if(linetmp[lowest+1] == '*')
- {
- if(linetmp[lowest+2] == '*')
- {
- if(lowest == 0)
- return(FCOMM); /* left aligned full width comment */
- else
- return(IFCOMM); /* indented full width comment */
- } /* end third character is asterisk */
- if(lowest == 0)
- return(LCOMM); /* left aligned comment line */
- else
- return(ICOMM); /* indented comment line */
- } /* end second character is asterisk */
- } /* end first character is slash */
-
- if(lowest == 0)
- {
- pindex = 0; /* force outer level */
- prior[0] = 0; /* reset current value */
- return(ZCODE); /* and show at left margin */
- }
-
- if(lowest < prior[pindex])
- {
- if(lowest < prior[pindex-1])
- {
- while(lowest < prior[pindex] )
- pindex--;
- prior[pindex] = lowest; /* save new address */
- return(O2CODE); /* and show out 2 levels */
- }
- else
- {
- while(lowest < prior[pindex] )
- pindex--;
- prior[pindex] = lowest; /* save new address */
- return(OCODE); /* code, decrease indentation */
- }
- }
-
- if(lowest > prior[pindex])
- {
- prior[++pindex] = lowest; /* save new address */
- return(ICODE); /* code, increase indentation */
- }
- else
- return(CODE); /* code, aligned */
-
- } /* end test line type */
-
- /****************************************************************************/
- /* */
- /* Expand tabs to eight position spaces, */
- /* remove carriage returns and newlines, if any, */
- /* and return count of data characters in line. */
- /* */
- /****************************************************************************/
-
- detab(work,in)
- char in[];
- char work[];
- {
- register int from = 0;
- register int to = 0;
- register int i = -1;
- register int c;
-
- while( c = in[from++] )
- {
- switch (c)
- {
- case '\t': /* for a tab, */
- do {
- work[to++] = ' '; /* stuff a space */
- } while ( to & 7); /* until the tab stop */
- break;
-
- case '\r': /* carriage return */
- break; /* kill them off */
-
- case '\n': /* newline */
- break; /* kill them too */
-
- case ' ': /* for spaces, */
- work[to++] = c; /* copy the character */
- break;
-
- default: /* for data, */
- work[to] = c; /* copy the character */
- i = to++; /* and save position */
- break;
- } /* end switch */
- } /* end while */
- work[to] = '\0'; /* insure terminator */
- return(i+1); /* return data count */
-
- } /* end detab */
-
- /****************************************************************************/
- /* */
- /* Process a program line, according to its type. */
- /* */
- /****************************************************************************/
-
- process(style,out,in,fp)
- int style;
- char in[];
- char out[];
- FILE *fp;
- {
- switch(style)
- {
- case BLANK: /* blank line */
- fprintf(fp,"\n"); /* write a blank */
- break;
-
- case FCOMM: /* full width comment line */
- comm_solid(out); /* fill to right edge of comment */
- fprintf(fp,"%s\n",out); /* write the line */
- break;
-
- case LCOMM: /* left aligned comment line */
- comm_fix(out,in); /* align right edge of comment */
- fprintf(fp,"%s\n",out); /* write the line */
- break;
-
- case IFCOMM: /* indented full width comment line */
- comm_block(out,in); /* align right edge of comment */
- fprintf(fp,"%s\n",out); /* write the line */
- break;
-
- case ICOMM: /* indented comment line */
- comm_fix(out,in); /* align right edge of comment */
- fprintf(fp,"%s\n",out); /* write the line */
- break;
-
- case CCOMM: /* continued comment */
- comm_sync(out,in); /* sync up the line */
- fprintf(fp,"%s\n",out); /* write it */
- break;
-
- case CODE: /* normal code line */
- code_left(out,in); /* align left edge of code */
- comm_align(in,out); /* align the comment */
- fprintf(fp,"%s\n",in); /* write the line */
- break;
-
- case ICODE: /* code: indentation increase */
- align++; /* increase skip count */
- code_left(out,in); /* align left edge of code */
- comm_align(in,out); /* align the comment */
- fprintf(fp,"%s\n",in); /* write the line */
- break;
-
- case OCODE: /* code: indentation decrease */
- if(align) /* if still indented, */
- align--; /* back up one */
- code_left(out,in); /* align left edge of code */
- comm_align(in,out); /* align the comment */
- fprintf(fp,"%s\n",in); /* write the line */
- break;
-
- case O2CODE: /* code: indentation decrease two */
- if(align) /* if still indented, */
- align--; /* back up one */
- if(align) /* if still indented, */
- align--; /* back up another */
- code_left(out,in); /* align left edge of code */
- comm_align(in,out); /* align the comment */
- fprintf(fp,"%s\n",in); /* write the line */
- break;
-
- case ZCODE: /* code: indentation to zero */
- align = 0; /* back to margin */
- code_left(out,in); /* align left edge of code */
- comm_align(in,out); /* align the comment */
- fprintf(fp,"%s\n",in); /* write the line */
- break;
-
- } /* end switch */
- } /* end process */
-